package com.woniu.backparking.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.backparking.dto.PageResult;
import com.woniu.backparking.entity.User;
import com.woniu.backparking.service.UserService;
import com.woniu.backparking.util.BaseEnum;
import com.woniu.backparking.util.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HashSong
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@Slf4j
public class UserController {
    @Resource
    private UserService userService;
    /*
    对所有用户的分页条件查询
    参数 分页条件 查找用户名 用户状态
    return 该页用户具体信息
     */
    @GetMapping("queryAll")
//    @Cacheable(cacheNames = "queryAllUser",key="#pageResult.current+#pageResult.name01+#pageResult.state01")
    public JSONResult queryAll(PageResult pageResult){
        log.info("用户表查询参数{}",pageResult);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(pageResult.getName01()!=null,"user_name",pageResult.getName01());
        queryWrapper.eq(pageResult.getState01()!=2,"STATUS",pageResult.getState01());
        Page<User> userPage = new Page<>();
        userPage.setCurrent(pageResult.getCurrent());
        userPage.setSize(pageResult.getPageSize());
        Page<User> page = userService.page(userPage, queryWrapper);
        pageResult.setPageNum((int) userPage.getPages());
        pageResult.setTotal((int)userPage.getTotal());
        pageResult.setCurrent((int)userPage.getCurrent());
        return new JSONResult(BaseEnum.StatusCode.Success, BaseEnum.StatusMessage.Success,pageResult,page.getRecords());
    }
    /*
    更改用户状态：封号或解封
    参数 用户id 用户目标状态
    return 无
     */
    @PutMapping("StatueUpdate")
//    @CacheEvict(cacheNames = "queryAllUser",key="#current+#name01+#state01")
    public JSONResult StatueUpdate(int current,String name01,int state01,int userId,int status){
        log.info("用户状态更改参数—{}-{}",userId,status);
        User user = new User();
        user.setUserId(userId);
        user.setStatus(status);
        userService.updateById(user);
        return new JSONResult(BaseEnum.StatusCode.Success, BaseEnum.StatusMessage.Success,null,null);
    }
    /*
    针对投诉对用户信誉度进行更改
    参数：userId 要改变的用户的id number 要改变的信誉度（只减
    return
     */
    @PutMapping("creditUpdate")
//    @CacheEvict(cacheNames = "queryAllUser",key="#current+#name01+#state01")
    public JSONResult creditUpdate(int userId,int value){
        log.info("更改用户信誉度参数:减少信誉度{}-ID{}",value,userId);
        User user = userService.getById(userId);
        Integer credit = user.getCredit();
        if(credit<=value){
            user.setCredit(0);
        }else {
            user.setCredit(credit-value);
        }
        userService.updateById(user);
        return new JSONResult(BaseEnum.StatusCode.Success, BaseEnum.StatusMessage.Success,null,null);
    }
}

